/*          ._
 *            '.			     _______ _______ ___ ___ _______ 
 *     --._     \    .-.		|   |   |_     _|   |   |    ___|
 *         '. .--;  /  |		|       |_|   |_|   |   |    ___|
 *         ,;/ ^  |`\  /.-"".   |___|___|_______|\_____/|_______|	
 *        ;' \  _/   |'    .'          code.google.com/p/as-hive
 *       /    `.;I> /_.--.` )    __
 *      |    .'`  .'( _.Y/`;    /  \__
 *       \_.'---'`   `\  `-/`-. \__/  \__
 *         /_-.`-.._ _/`\  ;-./_/  \__/  \
 *        |   -./  ;.__.'`\  /  \__/  \__/
 *         `--'   (._ _.'`|  \__/  \__/
 *                /     ./ __/  \__/  \
 *           jgs ; `--';'_/  \__/  \__/
 *               `;-,-'/  \__/  \
 *               ,'`   \__/  \__/
 *
 * Copyright 2007 (c) Jovan Alleyne, Peter Nitsch, Brandon Flowers.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package org.hive.data
{
	import flash.xml.XMLDocument;
	import flash.events.*;
    import flash.net.*;
    import org.hive.events.PropertiesEvent;

	public dynamic class Properties extends EventDispatcher
	{
        /**
        *
        */
		private var __loader:URLLoader;

        /**
        *
        */
		private var __source:String;

        /**
        *
        */
		private var __propertiesXML:XML = new XML( );
		
		public function get data():XML {
			trace(__propertiesXML.children().toXMLString());
			return __propertiesXML;
		}
		
        /**
        *
        * @param	source
        */
		public function Properties( source:String )
		{
			__source = source;
			__loader = new URLLoader( );
			
			__loader.addEventListener( Event.COMPLETE, completeHandler );
            __loader.addEventListener( Event.OPEN, openHandler );
            __loader.addEventListener( ProgressEvent.PROGRESS, progressHandler );
            __loader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
            __loader.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );
            __loader.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
		}
		
        /**
        *
        * @return
        */
		public function load( ):void
		{
			try {
                __loader.load( new URLRequest( __source )  );
            } catch ( error:Error ) {
                trace("Unable to load requested document.");
            }
		}
		
		
        /**
        *
        * @param	event
        * @return
        */
		private function completeHandler(event:Event):void
		{
    		__propertiesXML = XML( __loader.data );
			for each (var p:XML in __propertiesXML.children() )
			{
				if( p.@type == 'Number' ){
					this[ p.localName( ) ] = parseInt( p.toString() ) ;
				}
				else {
					this[ p.localName( ) ] = p.toString();
				}
			}
			
			var propertiesEvent :PropertiesEvent= new PropertiesEvent( PropertiesEvent.PROPERTIES_READY, this );
			dispatchEvent( propertiesEvent );
        }

        /**
        *
        * @param	event
        * @return
        */
        private function openHandler(event:Event):void
        {
            //trace("openHandler: " + event);
        }

        /**
        *
        * @param	event
        * @return
        */
        private function progressHandler(event:ProgressEvent):void
        {
           //trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        /**
        *
        * @param	event
        * @return
        */
        private function securityErrorHandler(event:SecurityErrorEvent):void
        {
            trace("securityErrorHandler: " + event);
        }

        /**
        *
        * @param	event
        * @return
        */
        private function httpStatusHandler(event:HTTPStatusEvent):void
        {
            //trace("httpStatusHandler: " + event);
        }

        /**
        *
        * @param	event
        * @return
        */
        private function ioErrorHandler(event:IOErrorEvent):void
        {
            trace("ioErrorHandler: " + event);
        }
		
	}
}